﻿$(document).ready(function () {
    if (parseInt(nPermissions) >= 3) {
        $("#btnsave").click(function () {
            if ($("#btnsave").val() == "Edit") {
                $('table[editcellcontent="true"]').InitialEditTableCell();
                $("#btnsave").val("Done");
                $(".changed").addClass("status");
            }
            else {
                if (lPolID <= 0) {
                    alert("Bạn chưa nhập POL.");
                    return;
                }
                setTimeout(function () {
                    $("#btnsave").val("Edit");
                    $(".changed").removeClass("status");
                    SaveChangeItems();
                }, 50);
            }
        });
    } else {
        $("#btnsave").remove();
    }
    $('*[zoomable="true"]').InitialZoom();
    //$('table[editcellcontent="true"]').SelectColumn('selectcolumn');
});

function BindbtnSave_Click() {


}

function SaveChangeItems() {
    var rowchanges = $('tr[changed="true"]');
    $('table[editcellcontent="true"]').DisableEditTableCell();
    if ($(rowchanges).length == 0) {
        return;
    }
    var ID = -1, DC20 = "", DC40 = "", HC40 = "", RF20 = "", RF40 = "", Validity = "", Note = "";

    var temp = "";
    var items = new Array();
    var changecells = new Array();
    var _cell;
    $.each(rowchanges, function (i, item) {
        ID = $(this).attr("priceid");

        _cell = $(this).children('td[code="DC20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            DC20 = Encoder.htmlEncode(temp);
        } else {
            DC20 = "";
        }

        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        _cell = $(this).children('td[code="DC40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            DC40 = Encoder.htmlEncode(temp);
        } else {
            DC40 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        _cell = $(this).children('td[code="HC40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            HC40 = Encoder.htmlEncode(temp);
        } else {
            HC40 = "";
        }
        //Validity
        _cell = $(this).children('td[code="Validity"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Validity = Encoder.htmlEncode(temp);
        } else {
            Validity = "";
        }
        _cell = $(this).children('td[code="ValidityEx"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            ValidityEx = Encoder.htmlEncode(temp);
        } else {
            ValidityEx = "";
        }

        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        _cell = $(this).children('td[code="RF20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            RF20 = Encoder.htmlEncode(temp);
        } else {
            RF20 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        _cell = $(this).children('td[code="RF40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            RF40 = Encoder.htmlEncode(temp);
        } else {
            RF40 = "";
        }
        //Note
        _cell = $(this).children('td[code="Note"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Note = Encoder.htmlEncode(temp);
        } else {
            Note = "";
        }
        _cell = $(this).children('td[code="NoteEx"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            NoteEx = Encoder.htmlEncode(temp);
        } else {
            NoteEx = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        _cell = $(this).children('td[code="SerAreaName"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            ServiceAreaName = Encoder.htmlEncode(temp);
        } else {
            ServiceAreaName = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        _cell = $(this).children('td[code="PodIncluded"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            PodIncluded = Encoder.htmlEncode(temp);
        } else {
            PodIncluded = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        items.push("{'acc':'PriceAreas','ID':'" + ID + "','DC20':'" + DC20 + "','DC40':'" + DC40 + "','HC40':'" + HC40 +
            "','Validity':'" + Validity + "','ValidityEx':'" + ValidityEx + "','RF20':'" + RF20 + "','RF40':'" + RF40 +
            "','Note':'" + Note + "','NoteEx':'" + NoteEx + "','ServiceAreaName':'" + ServiceAreaName + "','PodIncluded':'" + PodIncluded +
             "','CarrID':'" + lCarrID + "','PolID':'" + lPolID + "','LastUpdate':'" + new Date().toDateString() + "','Sort':'0','UserID':'-1'}");
    });
    $.ajax({
        type: 'POST',
        url: "Handlers/UpdatePriceAreas.ashx?acc=PriceAreas",
        data: { items: "[" + items.toString() + "]" },
        success: function (data) {
            if (data == "access denied!") {
            } else if (data == "not valid") {

            } else {

                $.each(changecells, function (i, item) {
                    $(item).addClass("has-value");
                    $(item).attr("title", data.UserName + ": " + data.Date);

                });

                var _newdate = data.Date.toString().replace(/-/g, '');
                $.each(rowchanges, function (i, item) {
                    $(item).children('*[code="update"]').text(_newdate);
                });
                $('tr[changed="true"]').removeAttr("changed");
            }
        },
        dataType: "json"
    });
}
